package chances.wechat.service.rdb;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import chances.wechat.entity.UserRequest;
import chances.wechat.entity.WechatHistory;
import chances.wechat.service.UserRequestService;

@Service("userRequestService")
public class RdbRequestService extends BaseDao implements UserRequestService {

	@Override
	public void insertRequest(UserRequest userReq) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", userReq);
		try {
			this.insert("insertUserReq", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<UserRequest> list(String userId, String type, int size) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("userId", userId);
		paramsMap.put("cmd", type);
		paramsMap.put("size", size);
		try {
			return (List<UserRequest>) this.queryForList("selectUserRequestByUserIdAndType", paramsMap, 0, size);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@Override
	public void dels(String[] codes, String userId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("codes", codes);
		paramsMap.put("userId", userId);
		try {
			this.delete("delPlayByUserAndCodes", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public List<WechatHistory> getAllUserHistory() {
		try {
			return (List<WechatHistory>) this.queryForList("selectUserHistory");
		} catch (Exception e) {}
		return null;
	}
	
	
	public UserRequest getUserReqType(String contentCode, String openId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("contentCode", contentCode);
		paramsMap.put("openId", openId);
		try {
			return (UserRequest) this.queryForObject("selectUserReqType", paramsMap);
		} catch(Exception e) {
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		return null;
	}
	
	
	public void updateUserHistory(String userId, String contentCode, String contentType) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("userId", userId);
		paramsMap.put("contentCode", contentCode);
		paramsMap.put("contentType", contentType);
		
		try {
			this.update("updateUserHistory", paramsMap);
		} catch(Exception e) {
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}
	
}
